#include "kukarsiudpserverdll.h"

KuKaRsiUdpServerDLL::KuKaRsiUdpServerDLL(){
    this->kukaSocket = new QUdpSocket();
    kukaSocket->bind(QHostAddress::Any,1301);
    RecvDataReadyRead = false;
    WriteDataReadySend = false;
}

KuKaRsiUdpServerDLL::~KuKaRsiUdpServerDLL(){
    delete kukaSocket;
    kukaSocket = nullptr;
}

void KuKaRsiUdpServerDLL::UdpReadData(){
    QByteArray arr;
    if(kukaSocket->hasPendingDatagrams()){
        arr.resize(kukaSocket->bytesAvailable());
        kukaSocket->readDatagram(arr.data(),arr.size(),&kukaAddr,&kukaPort);
        RecvData = arr;
        RecvDataReadyRead = true;
    }
}

void KuKaRsiUdpServerDLL::UdpWriteData(){
    if(WriteDataReadySend == true){
        kukaSocket->writeDatagram(SendData,kukaAddr,kukaPort);
        RecvDataReadyRead = false;
        WriteDataReadySend = false;
    }
}

void KuKaRsiUdpServerDLL::KuKaDataRead(bool &ReadDone, QByteArray& Data){
    ReadDone = this->RecvDataReadyRead;
    if(this->RecvDataReadyRead == true){
        Data = this->RecvData;
    }
    else{
        Data = "<Note>NoMessage</Note>";
    }
}

void KuKaRsiUdpServerDLL::KuKaDataWrite(QByteArray Data){
    if(RecvDataReadyRead == true){
        this->SendData = Data;
        this->WriteDataReadySend = true;
    }
    else{
        this->WriteDataReadySend = false;
    }
}


